package com.jerry.consumer.controller;

import com.jerry.common.bean.Ad;
import com.jerry.consumer.client.AdClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/shop/ad")
public class AdController {

	@Value("${provider.adUrl}")
	private String adUrl;

	@Autowired
	private DiscoveryClient discoveryClient;
	@Autowired
	private AdClient adClient;
	
	//查询广告列表
	@SuppressWarnings("unchecked")
	@GetMapping("/list")
	public List<Ad> getAdList() {
		List<Ad> adList = adClient.list();
		return adList;
	}

	//根据id查询广告
	@GetMapping("/get/{id}")
	public Ad getAd(@PathVariable("id") Long id) {
		return adClient.get(id);
	}

	//增加一个广告
	@GetMapping("/add")
	public boolean addAd(Ad ad) {
		return adClient.add(ad);
	}

	//增加一个广告
	@GetMapping("/update")
	public boolean updateAd(Ad ad) {
		return adClient.update(ad);
	}

	//删除一个广告
	@GetMapping("/delete/{id}")
	public void deleteAd(@PathVariable("id") Long id) {
		adClient.delete(id);
	}

	//查询provider-user的实例信息
	@GetMapping("/adInstance")
	public List<ServiceInstance> userInstance() {
		return this.discoveryClient.getInstances("provider-ad");
	}


}
